package com.sec.android.app.myfiles.util;

import android.content.Context;
import android.drm.DrmInfo;
import android.drm.DrmInfoRequest;
import android.drm.DrmManagerClient;

import com.sec.android.app.myfiles.info.MediaFile;
import com.sec.android.app.myfiles.log.Log;

import java.util.Locale;

public class DrmUtils {
    private static final String TAG = "DrmUtils";
    private static final String OMADRM_MIMETYPE = "application/vnd.oma.drm.content";
    private static final String PLAYREADY_PYV_MIMETYPE = "video/vnd.ms-playready.media.pyv";
    private static final String PLAYREADY_PYA_MIMETYPE = "audio/vnd.ms-playready.media.pya";
    private static final String PLAYREADY_WMA_MIMETYPE = "audio/x-ms-wma";
    private static final String PLAYREADY_WMV_MIMETYPE = "video/x-ms-wmv";
    private static DrmManagerClient mDrmManager = null;

    public static boolean isPlayReadyFile(Context context, String path) {
        if (path == null) {
            Log.w(TAG, "isPlayReadyFile() - path is null");
            return false;
        }
        String mimeType = MediaFile.getMimeType(path);
        if (PLAYREADY_PYA_MIMETYPE.equals(mimeType) || PLAYREADY_PYV_MIMETYPE.equals(mimeType) || PLAYREADY_WMA_MIMETYPE.equals(mimeType)
                || PLAYREADY_WMV_MIMETYPE.equals(mimeType)) {
            try {
                if (getDrmManager(context).canHandle(path, mimeType)) {
                    Log.d(TAG, "isPlayReadyFile : this file is PlayReady => " + path);
                    return true;
                }
            } catch (IllegalArgumentException e) {
                Log.e(TAG, e.toString());
            }
        }
        return false;
    }

    private static DrmManagerClient getDrmManager(Context context) {
        if (mDrmManager == null) {
            mDrmManager = new DrmManagerClient(context.getApplicationContext());
        }
        return mDrmManager;
    }

    public static boolean isDcfFile(String filePath) {
        boolean bRet = false;
        if (filePath != null) {
            bRet = filePath.toUpperCase(Locale.getDefault()).endsWith(".DCF");
        }
        return bRet;
    }

    public static boolean isOMADrmFile(Context context, String filePath) {
        if (filePath == null) {
            Log.d(TAG, "isOMADrmFile : filePath is null.");
            return false;
        }

        if (isDcfFile(filePath)) {
            try {
                if (getDrmManager(context).canHandle(filePath, OMADRM_MIMETYPE)) {
                    Log.d(TAG, "isOMADrmFile : this file is DRM. => ");
                    return true;
                }
            } catch (IllegalArgumentException e) {
                Log.e(TAG, "IllegalArgumentException:" + e.toString());
            }
        }
        return false;
    }

    public static boolean isForwardable(Context context, String filePath) {
        if (filePath == null) {
            Log.d(TAG, "isForwardable : filePath is null.");
            return false;
        }
        if (isPlayReadyFile(context, filePath)) {
            Log.d(TAG, "isForwardable => " + "this is not forwardable.");
            return false;
        } else if (isOMADrmFile(context, filePath)) {
            if (!isPossibleSend(context, filePath)) {
                Log.d(TAG, "isForwardable => " + "this is not forwardable.");
                return false;
            }
        }
        return true;
    }

    private static boolean isPossibleSend(Context context, String filePath) {
        boolean bRet = true;
        return bRet;
    }

    public static String getRealMimeTypeOfDRM(Context context, String filePath) {
        if (filePath == null) {
            Log.w(TAG, "getRealMimeTypeOfDRM : filePath is null.");
            return null;
        }
        String mimeType = getDrmManager(context).getOriginalMimeType(filePath);

        return mimeType;

    }

    public static boolean isDRMFile(Context context, String filePath) {
        if (isOMADrmFile(context, filePath)) {
            return true;
        } else if (isPlayReadyFile(context, filePath)) {
            return true;
        }
        return false;
    }

}
